<?php

namespace app\api\controller\eid;

use app\Request;
use think\facade\Db;
use think\facade\Env;
use think\response\Json;
use crmeb\exceptions\AdminException;
use think\exception\ValidateException;
use app\services\eid\EidTokenServices;
use app\api\controller\RestBaseController;
use app\services\worker\RepairWorkerServices;
use app\services\eid\GetEidResultServices;
use think\db\exception\{DataNotFoundException, DbException, ModelNotFoundException};

class EidTokenController extends RestBaseController
{
    /** @var EidTokenServices */
    protected $services;

    /**
     * @return void
     */
    protected function initialize()
    {
        parent::initialize();
        $this->services = app()->make(EidTokenServices::class);
    }

    /**
     * 获取 EidToken
     * @return Json
     */
    public function getEidToken(): Json
    {
        try {
            $res = $this->services->getEidToken();
        } catch (AdminException $e) {
            $this->error($e->getMessage());
        }

        $this->success("获取E证通Token成功!", $res);
    }

    /**
     * 获取E证通结果信息
     * @param Request $request
     * @param GetEidResultServices $getEidResultServices
     * @return Json
     */
    public function getEidResult(Request $request, GetEidResultServices $getEidResultServices): Json
    {
        $userId = $this->getUserId();
        halt($userId);
        $data = $request->postMore([
            ['EidToken', ''],
        ]);
        try {
            $res = $getEidResultServices->getEidResult($data['EidToken']);
        } catch (AdminException $e) {
            $this->error($e->getMessage());
        }

        $this->success("获取E证通结果信息!", $res);
    }
}
